//
//  CZTestRefreshControl.swift
//  Weibo28
//
//  Created by Apple on 16/5/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum TestRefreshState: Int {
    //  下拉刷新
    case Normal = 0
    //  松手就刷新
    case Pulling = 1
    //  正在刷新
    case Refreshing = 2
}

class CZTestRefreshControl: UIControl {
    //  记录当前的刷新状态
    var testRefreshState: TestRefreshState = .Normal {
        didSet{
            
            switch testRefreshState {
            
            case .Normal:
                print(".noraml")
                // 只有上一次是刷新状态才去-=50
                if oldValue == .Refreshing {
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.currentScrollView?.contentInset.top -= 50
                    })
                }
               
                
            case .Pulling:
                print(".pulling")
            case .Refreshing:
                print("refreshing")
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.currentScrollView?.contentInset.top += 50
                })
                
                //  告诉使用者要刷新数据
                sendActionsForControlEvents(.ValueChanged)
                
                
            }
            
            
            
        }
    }

    var currentScrollView: UIScrollView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.yellowColor()
    }
    //  获取当前控件添加到那个父视图控件上
    override func willMoveToSuperview(newSuperview: UIView?) {
        print("------------")
        print(newSuperview)
        //  判断我们的视图是否添加在我们的滚动视图上
        guard let scrollView = newSuperview as? UIScrollView else {
            return
        }
        //  添加kvo监听
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: [.New], context: nil)
        currentScrollView = scrollView
        
        //  设置frame
        frame.size.height = 50
        frame.size.width = currentScrollView!.frame.size.width
        frame.origin.y = -50
        
        
    }
    //  kvo监听方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    
        
        
        
        
        
        if currentScrollView!.dragging {
            //  拖动情况下只有两种状态 1 下拉刷新状态(normal), 2 松手就刷新(Pulling)
            
            let maxY = -(currentScrollView!.contentInset.top + 50)
           
            let contentOffsetY = currentScrollView!.contentOffset.y
            //  判断当前的偏移量 和 我们maxY 对比
            
            if contentOffsetY < maxY && testRefreshState == .Normal {
                //  进入pulling状态
                testRefreshState = .Pulling

            } else if contentOffsetY >= maxY && testRefreshState == .Pulling {
                testRefreshState = .Normal

            }
        
            
            
            
            
            
            //  拖动

        } else {
              //  要想进入正在刷新的状态(refreshing), 1. 上一次是松手就刷新的状态,并且 还要松手
            //  松手

            
            if testRefreshState == .Pulling {
                //  正在刷新状态
                testRefreshState = .Refreshing
            }
            
        }
        
        
      
        
        
        
    }
    
    func endRefreshing() {
        testRefreshState = .Normal
    }
    
    deinit {
        //  移除kvo
        currentScrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
    
    
    
    

}
